unit U_datamod;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Db, ADODB;

type
  TDM_Main = class(TDataModule)
    Database1: TADOConnection;
    Query1: TADOQuery;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  DM_Main: TDM_Main;

  function StartMulSelect(user,pass,serv,sqlstr:pchar;var inSelectValues:TStrings; isOnlyOne:Boolean=false):TStrings;stdcall;
  function GetStringsList(user,pass,serv,sqlstr:pchar):TStrings;stdcall;

    function GetADOConnectString(user, pass, serv: String): String;
    procedure QueryClose(dataset: TDataset);
    procedure setDatabaseParam(u, p, s: String);

implementation

uses mulselect;

{$R *.DFM}

function GetADOConnectString(user,pass,serv:String):String;
begin
  Result:='Provider=MSDAORA.1;Persist Security Info=True;'
         +'User ID='+user+';Password='+pass+';Data Source='+serv;
end;

procedure QueryClose(dataset: TDataset);
begin
    if not dataset.Active then exit;
    try
      if (dataset is TADOQuery) and (dataset.Eof) then
          dataset.Last
      else if (dataset is TADOQuery) and (dataset.Bof) then
          dataset.First;
      dataset.Close;
    except end;
end;

procedure setDatabaseParam(u,p,s:String);
var con:String;
begin
    con:=DM_Main.Database1.ConnectionString;
    If (pos(';User ID='+u+';',con)>0)
       and (pos(';Password='+p+';',con)>0)
       and (pos(';Data Source='+s,con)>0)
     Then
       exit;
    DM_Main.Database1.Connected:=false;
    DM_Main.Database1.ConnectionString:=GetADOConnectString(u,p,s);
 
end;

function StartMulSelect(user,pass,serv,sqlstr:pchar;var inSelectValues:TStrings; isOnlyOne:Boolean=false):TStrings;
var selectValues:TStrings;
begin
   setDatabaseParam(user,pass,serv);

   DM_Main.Query1.Close;
   DM_Main.Query1.SQL.Text:=sqlstr;
   DM_Main.Query1.Open;
   selectValues:=MulSelectForm.StartSelect(DM_Main.Query1,inSelectValues,isOnlyOne);
   result:=selectValues;
end;

function GetStringsList(user,pass,serv,sqlstr:pchar):TStrings;
var 
s:String;
r:TStrings;
begin
r:=TStringList.Create;
If sqlstr='' Then Exit;
setDatabaseParam(user,pass,serv);
    ShowMessage(sqlstr);
try
    DM_Main.Query1.Close;
    DM_Main.Query1.SQL.Text:=sqlstr;
    DM_Main.Query1.Open;
    while Not DM_Main.Query1.Eof do
    begin
        s:=DM_Main.Query1.fields[0].AsString;
        R.Add(s);
        DM_Main.Query1.Next;
    end;
    QueryClose(DM_Main.Query1);
except 
end;
    result:=r;
end;


end.
